/*
 * VariedadeCadastrar.java
 *
 * Created on 20/02/2009, 16:31:14
 * Copyright Jaime Junior
 */
package Adecot.Produtos;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlMessage;
import com.icesoft.faces.component.ext.HtmlOutputText;
import com.icesoft.faces.component.jsfcl.data.PopupBean;
import com.icesoft.faces.component.panelpopup.PanelPopup;
import com.icesoft.faces.component.selectinputtext.SelectInputText;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import javax.faces.FacesException;
import Adecot.ApplicationBean1;
import Adecot.RequestBean1;
import Adecot.SessionBean1;
import Controller.Facade.IceDecorator;
import Controller.Facade.ValidaFacade;
import Controller.VariedadeBO;
import Model.Variedade;
import com.icesoft.faces.component.jsfcl.data.DefaultSelectionItems;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

public class VariedadeCadastrar extends AbstractPageBean {
   // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

   private int __placeholder;

   private ValidaFacade validacao;
   private DefaultSelectionItems item;

   private boolean parar;
   public DefaultSelectionItems getItem() {
      return item;
   }

   public void setItem(DefaultSelectionItems item) {
      this.item = item;
   }

   /**
    * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
    * This method is automatically generated, so any user-specified code inserted
    * here is subject to being replaced.</p>
    */
   private void _init() throws Exception {
   }
   private PopupBean panelPopup1Bean = new PopupBean();

   public PopupBean getPanelPopup1Bean() {
      return panelPopup1Bean;
   }

   public void setPanelPopup1Bean(PopupBean pb) {
      this.panelPopup1Bean = pb;
   }
   private PopupBean panelPopup1Bean1 = new PopupBean();

   public PopupBean getPanelPopup1Bean1() {
      return panelPopup1Bean1;
   }

   public void setPanelPopup1Bean1(PopupBean pb) {
      this.panelPopup1Bean1 = pb;
   }
   private PopupBean panelPopup1Bean2 = new PopupBean();

   public PopupBean getPanelPopup1Bean2() {
      return panelPopup1Bean2;
   }

   public void setPanelPopup1Bean2(PopupBean pb) {
      this.panelPopup1Bean2 = pb;
   }
   private PanelPopup painelConsulta = new PanelPopup();

   public PanelPopup getPainelConsulta() {
      return painelConsulta;
   }

   public void setPainelConsulta(PanelPopup pp) {
      this.painelConsulta = pp;
   }
   private PanelPopup painelSair = new PanelPopup();

   public PanelPopup getPainelSair() {
      return painelSair;
   }

   public void setPainelSair(PanelPopup pp) {
      this.painelSair = pp;
   }
   private PanelPopup painelAviso = new PanelPopup();

   public PanelPopup getPainelAviso() {
      return painelAviso;
   }

   public void setPainelAviso(PanelPopup pp) {
      this.painelAviso = pp;
   }
   private PopupBean panelPopup1Bean3 = new PopupBean();

   public PopupBean getPanelPopup1Bean3() {
      return panelPopup1Bean3;
   }

   public void setPanelPopup1Bean3(PopupBean pb) {
      this.panelPopup1Bean3 = pb;
   }
   private PanelPopup painelMensagem = new PanelPopup();

   public PanelPopup getPainelMensagem() {
      return painelMensagem;
   }

   public void setPainelMensagem(PanelPopup pp) {
      this.painelMensagem = pp;
   }
   private HtmlCommandButton botaoConsultar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultar() {
      return botaoConsultar;
   }

   public void setBotaoConsultar(HtmlCommandButton hcb) {
      this.botaoConsultar = hcb;
   }
   private HtmlCommandButton botaoNovo = new HtmlCommandButton();

   public HtmlCommandButton getBotaoNovo() {
      return botaoNovo;
   }

   public void setBotaoNovo(HtmlCommandButton hcb) {
      this.botaoNovo = hcb;
   }
   private HtmlCommandButton botaoAlterar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoAlterar() {
      return botaoAlterar;
   }

   public void setBotaoAlterar(HtmlCommandButton hcb) {
      this.botaoAlterar = hcb;
   }
   private HtmlCommandButton botaoImprimir = new HtmlCommandButton();

   public HtmlCommandButton getBotaoImprimir() {
      return botaoImprimir;
   }

   public void setBotaoImprimir(HtmlCommandButton hcb) {
      this.botaoImprimir = hcb;
   }
   private HtmlCommandButton botaoLimpar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoLimpar() {
      return botaoLimpar;
   }

   public void setBotaoLimpar(HtmlCommandButton hcb) {
      this.botaoLimpar = hcb;
   }
   private HtmlInputText nome = new HtmlInputText();

   public HtmlInputText getNome() {
      return nome;
   }

   public void setNome(HtmlInputText hit) {
      this.nome = hit;
   }
   private HtmlCommandButton botaoCadastrar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoCadastrar() {
      return botaoCadastrar;
   }

   public void setBotaoCadastrar(HtmlCommandButton hcb) {
      this.botaoCadastrar = hcb;
   }
   private HtmlCommandButton botaoAvisoSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoAvisoSim() {
      return botaoAvisoSim;
   }

   public void setBotaoAvisoSim(HtmlCommandButton hcb) {
      this.botaoAvisoSim = hcb;
   }
   private HtmlCommandButton botaoAvisoNao = new HtmlCommandButton();

   public HtmlCommandButton getBotaoAvisoNao() {
      return botaoAvisoNao;
   }

   public void setBotaoAvisoNao(HtmlCommandButton hcb) {
      this.botaoAvisoNao = hcb;
   }
   private HtmlOutputText msgAvisoResultado = new HtmlOutputText();

   public HtmlOutputText getMsgAvisoResultado() {
      return msgAvisoResultado;
   }

   public void setMsgAvisoResultado(HtmlOutputText hot) {
      this.msgAvisoResultado = hot;
   }
   private HtmlCommandButton botaoMensagemOk = new HtmlCommandButton();

   public HtmlCommandButton getBotaoMensagemOk() {
      return botaoMensagemOk;
   }

   public void setBotaoMensagemOk(HtmlCommandButton hcb) {
      this.botaoMensagemOk = hcb;
   }
   private HtmlOutputText msgMensagemOperacao = new HtmlOutputText();

   public HtmlOutputText getMsgMensagemOperacao() {
      return msgMensagemOperacao;
   }

   public void setMsgMensagemOperacao(HtmlOutputText hot) {
      this.msgMensagemOperacao = hot;
   }
   private HtmlOutputText msgMensagemCorpo = new HtmlOutputText();

   public HtmlOutputText getMsgMensagemCorpo() {
      return msgMensagemCorpo;
   }

   public void setMsgMensagemCorpo(HtmlOutputText hot) {
      this.msgMensagemCorpo = hot;
   }
   private HtmlCommandButton botaoSairSim = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSairSim() {
      return botaoSairSim;
   }

   public void setBotaoSairSim(HtmlCommandButton hcb) {
      this.botaoSairSim = hcb;
   }
   private HtmlCommandButton botaoSairNao = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSairNao() {
      return botaoSairNao;
   }

   public void setBotaoSairNao(HtmlCommandButton hcb) {
      this.botaoSairNao = hcb;
   }
   private HtmlOutputText msgPainelSair = new HtmlOutputText();

   public HtmlOutputText getMsgPainelSair() {
      return msgPainelSair;
   }

   public void setMsgPainelSair(HtmlOutputText hot) {
      this.msgPainelSair = hot;
   }
   private SelectInputText campoBuscaVariedade = new SelectInputText();

   public SelectInputText getCampoBuscaVariedade() {
      return campoBuscaVariedade;
   }

   public void setCampoBuscaVariedade(SelectInputText sit) {
      this.campoBuscaVariedade = sit;
   }
   private HtmlCommandButton botaoConsultaOk = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultaOk() {
      return botaoConsultaOk;
   }

   public void setBotaoConsultaOk(HtmlCommandButton hcb) {
      this.botaoConsultaOk = hcb;
   }
   private HtmlCommandButton botaoConsultaCancelar = new HtmlCommandButton();

   public HtmlCommandButton getBotaoConsultaCancelar() {
      return botaoConsultaCancelar;
   }

   public void setBotaoConsultaCancelar(HtmlCommandButton hcb) {
      this.botaoConsultaCancelar = hcb;
   }
   private HtmlCommandButton botaoSair = new HtmlCommandButton();

   public HtmlCommandButton getBotaoSair() {
      return botaoSair;
   }

   public void setBotaoSair(HtmlCommandButton hcb) {
      this.botaoSair = hcb;
   }
   private HtmlMessage message1 = new HtmlMessage();

   public HtmlMessage getMessage1() {
      return message1;
   }

   public void setMessage1(HtmlMessage hm) {
      this.message1 = hm;
   }


   // </editor-fold>
   /**
    * <p>Construct a new Page bean instance.</p>
    */
   public VariedadeCadastrar() {
   }

   /**
    * <p>Callback method that is called whenever a page is navigated to,
    * either directly via a URL, or indirectly via page navigation.
    * Customize this method to acquire resources that will be needed
    * for event handlers and lifecycle methods, whether or not this
    * page is performing post back processing.</p>
    *
    * <p>Note that, if the current request is a postback, the property
    * values of the components do <strong>not</strong> represent any
    * values submitted with this request.  Instead, they represent the
    * property values that were saved for this view when it was rendered.</p>
    */
   @Override
   public void init() {
      // Perform initializations inherited from our superclass
      super.init();
      // Perform application initialization that must complete
      // *before* managed components are initialized
      // TODO - add your own initialiation code here

      // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
      // Initialize automatically managed components
      // *Note* - this logic should NOT be modified
      try {
         _init();
         
      } catch (Exception e) {
         log("VariedadeCadastrar Initialization Failure", e);
         throw e instanceof FacesException ? (FacesException) e : new FacesException(e);
      }
      try {
          
         validacao = new ValidaFacade();
      } catch (Exception ex) {
         
      }
      parar = false;
      painelSair.setRendered(false);
      painelConsulta.setRendered(false);
      painelAviso.setRendered(false);
      painelMensagem.setRendered(false);

      limparCampos();
   // </editor-fold>
   // Perform application initialization that must complete
   // *after* managed components are initialized
   // TODO - add your own initialization code here
   }

   /**
    * <p>Callback method that is called after the component tree has been
    * restored, but before any event processing takes place.  This method
    * will <strong>only</strong> be called on a postback request that
    * is processing a form submit.  Customize this method to allocate
    * resources that will be required in your event handlers.</p>
    */
   @Override
   public void preprocess() {
   }

   /**
    * <p>Callback method that is called just before rendering takes place.
    * This method will <strong>only</strong> be called for the page that
    * will actually be rendered (and not, for example, on a page that
    * handled a postback and then navigated to a different page).  Customize
    * this method to allocate resources that will be required for rendering
    * this page.</p>
    */
   @Override
   public void prerender() {
   }

   /**
    * <p>Callback method that is called after rendering is completed for
    * this request, if <code>init()</code> was called (regardless of whether
    * or not this was the page that was actually rendered).  Customize this
    * method to release resources acquired in the <code>init()</code>,
    * <code>preprocess()</code>, or <code>prerender()</code> methods (or
    * acquired during execution of an event handler).</p>
    */
   @Override
   public void destroy() {
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected ApplicationBean1 getApplicationBean1() {
      return (ApplicationBean1) getBean("ApplicationBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected RequestBean1 getRequestBean1() {
      return (RequestBean1) getBean("RequestBean1");
   }

   /**
    * <p>Retornar uma referência para o Bean de dados limitados.</p>
    *
    * @retorna uma referência para o bean de dados limitados
    */
   protected SessionBean1 getSessionBean1() {
      return (SessionBean1) getBean("SessionBean1");
   }

 //ação dos botões do painel de opções
   public String botaoSair_action() {
      painelSair.setRendered(true);
      return null;
   }

   public String botaoConsultar_action() {
      painelConsulta.setRendered(true);
      return null;
   }

   public String botaoCancelar_action() {
      painelConsulta.setRendered(false);
      painelAviso.setRendered(false);
      return null;
   }      

   public String botaoCadastrar_action() {
      try {
         Variedade variedade = (Variedade) getBean("Variedade");
         System.out.println(variedade.getNome());
         System.out.println(variedade.getCodVariedade());
          new IceDecorator(VariedadeBO.class).salvar(variedade);
         limparCampos();
         msgMensagemCorpo.setValue("Operação Efetuada Com Sucesso");
         painelMensagem.setRendered(true);
         
      } catch (Exception e) {
         msgMensagemCorpo.setValue("Erro Ao Efetuar Operação");
         painelMensagem.setRendered(true);
      }
      return null;
   }

   public String botaoLimpar_action() {
      limparCampos();
      return null;
   }

   public String botaoNovo_action() {
      limparCampos();
      return null;
   }

   public String botaoAlterar_action() {

      habilitarCampos();
      return null;
   }


   //método para uso dos botões

   public void limparCampos() {
      FacesContext.getCurrentInstance().getExternalContext().getRequestMap().remove("Variedade");
      nome.resetValue();
      campoBuscaVariedade.resetValue();
      parar = false;
      habilitarCampos();
   }

   public void habilitarCampos() {
      nome.setDisabled(false);
   }

   public void desabilitarCampos() {
      nome.setDisabled(true);
   }


   //ação dos campos
   public void campoBuscaVariedade_processValueChange(ValueChangeEvent vce) {
      try {
         setItem(new IceDecorator(VariedadeBO.class).consultaIncremental("nome", campoBuscaVariedade.getValue().toString()));
      } catch (Exception ex) {
         
      }
   }    

   //ação dos botões dos popu ups
    public String botaoSairSim_action() {
        //return null means stay on the same page
        return "menu";
    }

    public String botaoSairNao_action() {
        painelSair.setRendered(false);
        return null;
    }

    public String botaoConsultaOk_action() {
        try{
        Variedade variedade = (Variedade) new IceDecorator(VariedadeBO.class).
                retornaObjeto("nome", campoBuscaVariedade.getValue().toString());
        limparCampos();
        setBean("Variedade", variedade);
        desabilitarCampos();
        painelConsulta.setRendered(false);
        parar = true;
     }catch(Exception e){

        painelAviso.setRendered(true);
        msgAvisoResultado.setValue("Não houve resultados para essa Consulta");

     }
      return null;
    }

    public String botaoConsultaCancelar_action() {
        painelConsulta.setRendered(false);
        return null;
    }

    public String botaoAvisoSim_action() {
        painelAviso.setRendered(false);
        return null;
    }

    public String botaoAvisoNao_action() {
        painelAviso.setRendered(false);
        painelConsulta.setRendered(false);
        return null;
    }

    public String botaoMensagemOk_action() {
        painelMensagem.setRendered(false);
        return null;
    }

    public void validateCampoUnico(FacesContext context, UIComponent component, Object value) {
      new ValidaFacade().validateCampoUnico(context, component, value, VariedadeBO.class, parar);
   }
}
